<template>
  <div class="content content51" v-if="initFlag">
    <el-checkbox v-model="form.strayXRay.isTest" :true-label="1" :false-label="0">稳定性检测不检测此类项目</el-checkbox>
    <div v-show="!form.strayXRay.isTest">
      <div class="innertitle">
        1．电子治疗时的杂散X射线
      </div>
      <div>
        入射表面垂直于参考轴，NTD= <el-input v-model="form.strayXRay.ntd" class="input-line" />cm；机架角度：0°线束系统：0°治疗床：0°。
      </div>
      <table border="1" cellspacing="0" cellpadding="0" align="center">
        <tr>
          <td>标称能量档</td>
          <td>限光筒尺寸</td>
          <td>探测器水下扫描深度（cm）</td>
          <td>结果</td>
        </tr>
        <tr v-for="trIndex in strayXRay.table.tr" :key="trIndex">
          <td><el-input v-model="form.strayXRay['energy_'+trIndex]" class="input-line" /></td>
          <td><el-input v-model="form.strayXRay['size_'+trIndex]" class="input-line" /></td>
          <td><el-input v-model="form.strayXRay['depth_'+trIndex]" class="input-line" /></td>
          <td><el-input v-model="form.strayXRay['result_'+trIndex]" class="input-line" /></td>
        </tr>
      </table>
      <div class="innertitle">
        2．X射线治疗时的相对表面剂量
      </div>
      <div class="input100">照射野：<el-radio-group v-model="form.relativeSurfaceDose.irradiationField">
    <el-radio label="30cm×30cm">30cm×30cm</el-radio>
    <el-radio :label="form.relativeSurfaceDose.irradiationField"><el-input v-model="form.relativeSurfaceDose.irradiationField" class="input-line" /></el-radio>
  </el-radio-group>；其他条件与“电子治疗时的杂散X射线”要求相同。</div>
      <table border="1" cellspacing="0" cellpadding="0" align="center">
        <tr>
          <td>标称能量档</td>
          <td>探测器水下扫描深度（cm）</td>
          <td>结果</td>
        </tr>
        <tr v-for="trIndex in relativeSurfaceDose.table.tr" :key="trIndex">
          <td><el-input v-model="form.relativeSurfaceDose['energy_'+trIndex]" class="input-line" /></td>
          <td><el-input v-model="form.relativeSurfaceDose['depth_'+trIndex]" class="input-line" /></td>
          <td><el-input v-model="form.relativeSurfaceDose['result_'+trIndex]" class="input-line" /></td>
        </tr>
      </table>
      <div class="innertitle">
        3．杂散中子 <span style="font-weight: normal;">
          <el-checkbox v-model="form.strayCenter.isTest" :true-label="1" :false-label="0">此项不检测，根据厂家提供的技术指标进行判断。</el-checkbox>
        </span>
      </div>
      <div v-show="!form.strayCenter.isTest">
        <div>杂散中子最大比值：<el-input v-model="form.strayCenter.max" class="input-line" />%；杂散中子平均比值：<el-input v-model="form.strayCenter.average" class="input-line" />%。</div>
        <div>标准要求：最大比值≤0.05%，平均值比≤0.02%。</div>
      </div>
    </div>
  </div>
</template>

<script>
  import {getInfoById, postInfo} from '@/api/testItem/item51';
  export default{
    name:'strayRadiationDetectionResults',
    props: {
    	// tab栏
      activeTab: {
    		type: String,
        default: "0",
      },
    	// 基本信息数据
      basicForm: {
    		type: Object,
        default: () => {
    			return {}
    		},
      },
    },
    data(){
      return{
        initFlag:false,
        strayXRay:{
          action:'strayXRay',
          flag:true,
          loading:false,
          notNumber:['isTest'],
          intNumber:[],
          like:false,
          table:{
            tr:8
          }
        },
        relativeSurfaceDose:{
          action:'relativeSurfaceDose',
          flag:true,
          loading:false,
          notNumber:['irradiationField'],
          intNumber:[],
          like:false,
          table:{
            tr:2
          }
        },
        strayCenter:{
          action:'strayCenter',
          flag:true,
          loading:false,
          notNumber:['isTest'],
          intNumber:[],
          like:false,
        },
        form:{
          strayXRay:{

          },
          relativeSurfaceDose:{

          },
          strayCenter:{

          }
        }
      }
    },
    watch:{
      activeTab:{
        handler(val) {
          this.$nextTick(()=>{
            if(val == 2){
              let resultId = this.basicForm.resultId;
              getInfoById(resultId).then(response => {
                if(response.code == 200){
                  let data = response.data;
                  for(let keys in data){
                    this.setData(keys,data[keys]);
                  }
                   setTimeout(()=>{
                     this.initFlag = true;
                   },300)
                }
              }).catch((response) => {
                 this.$emit("loadingchange", true);
               });
               }
            });
        },
        deep: true,
        immediate: true
      },
      "form.strayXRay":{
        handler(newName, oldName) {
         if(this.initFlag){
          this.validateData(newName,'strayXRay');
         }
        },
        deep:true,
      },
      "form.relativeSurfaceDose":{
        handler(newName, oldName) {
         if(this.initFlag){
          this.validateData(newName,'relativeSurfaceDose');
         }
        },
        deep:true,
      },
      "form.strayCenter":{
        handler(newName, oldName) {
         if(this.initFlag){
          this.validateData(newName,'strayCenter');
         }
        },
        deep:true,
      },
    },
    methods:{
      //修改数据
      setData(name,data){
        this.form[name] = data ? data : (this[name] ? {}:null);
      },
      //表单修改提交后端
       postForm(param,name){
         param['itemsId'] = this.form.itemsId;
         this.$emit("loadingchange", true);
      	 postInfo(param,this[name].action).then(res=>{
      		 this.$emit("loadingchange", false);
      		 if(res.code == 200){
      			 this[name].flag = false;
      			 if(name != 'sdd'){
      				this.setData(name,res.data);
      			 }
      		 }
      	 })
      },
      validateType(arr,keys,obj){
      	if(obj.like){
      		for(let i=0;i<arr.length;i++){
      			if(keys.indexOf(arr[i]) != -1){
      				return true;
      			}
      		}
      		return false;
      	}else{
      		if(arr.indexOf(keys) != -1){
      			return true;
      		}
      		return false;
      	}
      },
       //数据验证
       validateData(newName,myName){
      	 if(!this[myName].noClear){
      		clearTimeout(this[myName].loading);
      	 }else{
      		 this[myName].noClear = false;
      	 }
         let regNumber = /^[-]?\d*([.](\d+))?$/;//小数验证正则
         let regRepNumber = /[^-?\d.,]/g;//将除小数外的符号消除正则
         let regInt = /^\d*$/;//整数验证正则
         let regRepInt = /[^-?\d,]/g;//将除小数外的符号消除正则
         let reg,regRep;
         let repStr = '';
      	 let intResult
         if(JSON.stringify(newName) != '{}'){
           //遍历该对象所有字段进行验证
           for(let keys in newName){
             //数字验证环节
      			 if((typeof newName[keys] == 'string') ||  (typeof newName[keys] == 'number')){
      				if(newName[keys] && !this.validateType(this[myName].notNumber,keys,this[myName])){
      					intResult = this.validateType(this[myName].intNumber,keys,this[myName]);
      					reg = intResult ? regInt:regNumber;
      				  let bool = !reg.test(newName[keys]);
      				  if(bool){
      						setTimeout(()=>{
      							intResult = this.validateType(this[myName].intNumber,keys,this[myName]);
      							regRep = intResult ? regRepInt:regRepNumber;
      									repStr = newName[keys] + '';
      							repStr =  repStr.replace(regRep,'');
      							if(repStr != (newName[keys]+'')){
      								if(!this[myName].noClear){
      									this[myName].noClear = true;
      								}
      								newName[keys] = repStr;
      							}
      						},100);
      						if(this[myName].flag){
      						 this[myName].flag = false;//如果数字验证不通过不需要请求后端
      						}
      				  }
      				}
      			 }
           }
      		 this.isPost(newName,myName);
         }
       },
       isPost(newName,myName){
      	 if(this[myName].flag){//是否请求后端判断
      	  let postBody = {};
      	  for(let i in newName){
      	    if(newName[i] !== null && newName[i] !== undefined){
      	      postBody[i] = newName[i];
      	    }
      	  }
      	  if(JSON.stringify(postBody) != '{}'){
      			this[myName].loading = setTimeout(()=>{
      				this.postForm(postBody,myName);
      			},800)
      	  }
      	 }else{
      		 this[myName].flag = true;
      	 }
       },
    },
  }
</script>

<style lang="scss" scoped>
  @import '../content.scss';

  .miniFont {
    font-size: 10px;
  }

  .miniFontCenter {
    margin-top: 8px;
  }

  .content51 {
    padding-inline: 0 !important;
    ::v-deep .el-input__suffix{
      display: none;
    }
    .latex{
      margin-top: 15px;
    }
    table {
      width: 100%;

      td {
        max-width: 100px;

        ::v-deep .el-checkbox__label {
          white-space: break-spaces;
        }
      }

      .el-input {
        width: 100% !important;
      }

      .multipleInput {
        .el-input {
          width: 50px !important;
        }
      }
    }
    .input100 {
      .el-input {
        width: 100px !important;
      }
    }

    .innertitle {
      display: block;
      margin-left: 0;
    }

    .table1 {
      .el-radio {
        display: flex;
        align-items: center;
      }
    }

    #hearderTR td:nth-child(7) {
      display: none;
    }
  }
</style>
